package com.fly.r2dbc.basics.web;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.fly.r2dbc.basics.entity.Customer;
import com.fly.r2dbc.basics.service.CustomerService;

import lombok.extern.slf4j.Slf4j;

/**
 * 
 * CustomerController
 * 
 * @author 00fly
 * @version [版本号, 2023-05-18]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Slf4j
@Controller
@RequestMapping("/customer")
public class CustomerController
{
    @Autowired
    CustomerService customerService;
    
    /**
     * String 日期转换为 Date
     * 
     * @param dataBinder
     * @see [类、类#方法、类#成员]
     */
    @InitBinder
    public void dataBinder(WebDataBinder dataBinder)
    {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        dataBinder.registerCustomEditor(Date.class, new CustomDateEditor(df, true));
    }
    
    /**
     * 新增/更新数据
     * 
     * @param customer
     * @return
     * @see [类、类#方法、类#成员]
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String add(@Valid
    @ModelAttribute("item") Customer customer, Errors errors, Model model)
    {
        if (errors.hasErrors())
        {
            StringBuilder errorMsg = new StringBuilder();
            for (ObjectError error : errors.getAllErrors())
            {
                errorMsg.append(error.getDefaultMessage()).append(" ");
            }
            if (errorMsg.length() > 0)
            {
                log.info("errors message={}", errorMsg);
            }
            return "/customer/show";
        }
        customerService.saveOrUpdate(customer);
        return "redirect:/customer/list";
    }
    
    /**
     * 删除数据
     * 
     * @param id
     * @return
     * @see [类、类#方法、类#成员]
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public String delete(@PathVariable Long id)
    {
        customerService.deleteById(id);
        return "redirect:/customer/list";
    }
    
    /**
     * 列表展示
     * 
     * @param model
     * @return
     * @see [类、类#方法、类#成员]
     */
    @GetMapping({"/", "/list"})
    public String list(@RequestParam(defaultValue = "1")
    int pageNo, Model model)
    {
        pageNo = Math.max(0, pageNo - 1);
        Pageable pageable = PageRequest.of(pageNo, 10);
        model.addAttribute("users", customerService.findBy(pageable));
        
        long total = customerService.count();
        model.addAttribute("total", total);
        model.addAttribute("pageNo", pageable.getPageNumber() + 1);
        model.addAttribute("totalPage", (total + 9) / 10);
        log.info("total: {}, page: {}/{} ", total, pageable.getPageNumber() + 1, (total + 9) / 10);
        return "index";
    }
    
    /**
     * 编辑数据
     * 
     * @param id
     * @param model
     * @return
     * @see [类、类#方法、类#成员]
     */
    @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
    public String update(@PathVariable Long id, Model model)
    {
        model.addAttribute("item", customerService.queryById(id));
        return "index";
    }
}
